<?php

function list_data_gaji($periode_id){
	$periode_gaji_id=get_periode_gaji() ;
	if($periode_id==0){
		$periode_id=$periode_gaji_id;
	}	
	if( $periode_gaji_id==$periode_id ){
		$kalkulasi_periode=0;
	}else{
		$kalkulasi_periode=$periode_id;
	}
	$nik_start = isset($_GET['nik_start']) ? sprintf('%07d',(int)$_GET['nik_start']) :'0000001'; 	
	$nik_end = (isset($_GET['nik_end']) AND (int) $_GET['nik_end'] !='0' )? sprintf('%07d',(int)$_GET['nik_end']) :'9999999'; 	

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'), 
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:55%'), 
		'  '=>array('style'=>'text-align:right;border-bottom:2px solid;width:5%'), 
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);/*
 
	$query = "
		SELECT * FROM karyawan a 
		WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 
		AND	(a.nik BETWEEN {$nik_start}	AND {$nik_end})
	";*/
	$query = "   select a.karyawan_id FROM pr_gaji a INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
		 WHERE a.periode_id={$periode_id} AND	(b.nik BETWEEN {$nik_start}	AND {$nik_end})  ORDER BY b.nik ASC ";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =35;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&periode_id={$periode_id}&nik_start={$nik_start}&nik_end={$nik_end}&task={$_GET['task']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 35; 
	while($ey = my_fetch_array($result)){
		$i++; 
		$karyawan_temp=$ey['karyawan_id'];
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$jabatan =  loaddata_jabatan($karyawan['jabatan_id']);  
		$query = " SELECT takehomepay FROM pr_gaji 
			WHERE periode_id={$kalkulasi_periode}
			AND karyawan_id={$ey['karyawan_id']}";
		$result1 = my_query($query);	
		$gajis = my_fetch_array($result1);
		$nominal_page = $gajis['takehomepay'];
		$grand_nominal +=$gajis['takehomepay'];
		$editproperty = array(
				'href'=>'javascript:;',
				'onclick'=>'javascript:window.open(\'index.php?com=report&task=slip_gaji&periode_id='.$periode_id.'&karyawan_id='.$ey['karyawan_id'].'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');',
				'rel'=>'facebox',
				'title'=>'Cetak'
		);
		$edit_button = button_icon( 'printer.gif' , $editproperty  );

		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  $karyawan['nik'] ,   
			'nama'=>  $karyawan['nama'],   
			'rp'=>  'Rp.',   
			'nomonal'=> position_text_align(  rp_format($nominal_page ) , 'right'),  
			'operasi'=> position_text_align(  $edit_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();	
	return form_view($periode_gaji_id,$periode_id).table_builder($header , $datas ,  35, false,$paging  );
}

function form_view($periode_gaji_id,$periode_id){
	$view = form_header( "..." , "..."  ); 
	$query_check_periode = " SELECT * FROM pr_periode where periode_id <= {$periode_gaji_id} ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('m-Y',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
	);
	$view .= '<input type="hidden"  value="'.$_GET['com'].'" name="com" />';
	$view .= form_field_display( form_dropdown($fperiode_id,$prs)   , "Periode Gaji"   );	
	$nik_start = isset($_GET['nik_start']) ? sprintf('%07d',(int)$_GET['nik_start']) :'0000001'; 	
	$nik_end = (isset($_GET['nik_end']) AND (int) $_GET['nik_end'] !='0' )? sprintf('%07d',(int)$_GET['nik_end']) :'9999999'; 	
	$form_start = '<input type="text" size="7" value="'.$nik_start.'" name="nik_start" />';
	$form_end = '<input type="text" size="7" value="'.$nik_end.'" name="nik_end" />';
	$view .= form_field_display( $form_start.' s/d '.$form_end  , "NIK Range"   );
		$submit = array(
		'value' => (  ' Check '  ),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 	 
	$cetak = array(
		'value' => (  ' Cetak '  ),
		'onclick' => 'javascript:location.href=\'index.php?com=report&task=txt_gaji_all&periode_id='.$periode_id.'&nik_start='.$_GET['nik_start'].'&nik_end='.$_GET['nik_end'].'\'', 
		'type'=>'button','class'=>'main_button'
	);
	$form_cetak= form_dynamic($cetak); 	 
	$view .= form_field_display( $form_submit .' '.$form_cetak   , "&nbsp;" ,  "" );
	$view .= form_footer( );
	$view = str_replace("method=\"post\"","method=\"get\"",$view);
	return $view;
}